from flask import Flask,render_template,request
from pymongo import MongoClient
import jieba
import re

app = Flask(__name__)

client = MongoClient('localhost',27017)
db = client.py97
qu_ar = db.qu_ar


@app.route('/')
def hello_world():
    return render_template('index.html')


@app.route('/queson')
def get_queson():
    res = qu_ar.count()
    return render_template('queson.html',counts=res)


@app.route('/ansoner')
def get_ansoner():
    queson_listall = []
    req = request.args.get('req')
    seg_list = jieba.cut_for_search(req)
    str_1 = ','.join(seg_list)
    queson_list = str_1.split(',')
    print(queson_list)
    for i in queson_list:
        find_list = qu_ar.find({'queson': {'$regex': i}})
        for item in find_list:
            queson_listall.append(item['queson'])
    queson_listall = list(set(queson_listall))
    # print(queson_listall)
    return render_template('ansoner.html',keyword=req,answers=queson_listall)

@app.route('/home')
def get_home():
    req = request.args.get('reqid')
    key = request.args.get('key')
    item = qu_ar.find_one({'queson': req})
    answer = item['answers']
    return render_template('home.html',req=req,answer=answer,key=key)


if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8080)
